/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * GrouponSearch.java
 *
 */
package com.gfinal.map.place;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gfinal.map.common.Constant;
import com.gfinal.map.util.HttpUtils;
import com.gfinal.map.util.Print;

/**
 * 团购信息检索服务
 * 
 * @author Gao Yudong (gyd@nbicc.com)
 * @date 2015-2-26
 */
public class GrouponSearch {
	private static final String TAG = GrouponSearch.class.getName();
	private static final int PAGE_SIZE = 10;

	private String query = null;
	private float lat = 0;
	private float lng = 0;
	private int radius = 0;
	private int pageNum = 0;
	private int totlePage = -1;
	private boolean isFilter = false;
	/*
	 * 团购类型，取值如下： 1: 餐饮 2：生活 3：娱乐 4：旅游住宿
	 */
	private int groupon_type = 1;
	/*
	 * 排序字段，取值如下： data_type：数据类型，默认排序 groupon_num：团购销量 groupon_price：团购价格
	 * groupon_start：团购开始时间 distance:按距离排序，只对中心点检索有效
	 */
	private String sort_name = "data_type";
	/*
	 * 排序规则，取值如下： 0: 从高到低 1：从低到高
	 */
	private int sort_rule = 0;

	public int getTotlePage() {
		return totlePage;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public void setLat(float lat) {
		this.lat = lat;
	}
	public void setLng(float lng) {
		this.lng = lng;
	}
	public void setRadius(int radius) {
		this.radius = radius;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public void setFilter(boolean isFilter) {
		this.isFilter = isFilter;
	}
	public void setGroupon_type(int groupon_type) {
		this.groupon_type = groupon_type;
	}
	public void setSort_name(String sort_name) {
		this.sort_name = sort_name;
	}
	public void setSort_rule(int sort_rule) {
		this.sort_rule = sort_rule;
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	private String createURI() {
		ArrayList params = new ArrayList();
		params.add(new BasicNameValuePair("ak", Constant.BAIDU_AK));
		params.add(new BasicNameValuePair("output", "json"));
		params.add(new BasicNameValuePair("scope", "2"));
		params.add(new BasicNameValuePair("page_size", String
				.valueOf(PAGE_SIZE)));
		params.add(new BasicNameValuePair("event", "groupon"));
		params.add(new BasicNameValuePair("query", query));
		params.add(new BasicNameValuePair("page_num", String.valueOf(pageNum)));
		params.add(new BasicNameValuePair("location", String.valueOf(lat) + ","
				+ String.valueOf(lng)));
		params.add(new BasicNameValuePair("radius", String.valueOf(radius)));
		params.add(new BasicNameValuePair("region", "全国"));
		if (isFilter) {
			String filter = "groupon_type:" + groupon_type + "|sort_name:"
					+ sort_name + "|sort_rule:" + sort_rule;
			params.add(new BasicNameValuePair("filter", filter));
		}
		return Constant.PLACE_EVENTSEARCH_URL + "?"
				+ URLEncodedUtils.format(params, Constant.CHARSET);
	}

	public ArrayList<GrouponBean> execute() throws IOException {
		ArrayList<GrouponBean> groupList = new ArrayList<GrouponBean>();
		String response = HttpUtils.doGet(createURI());
		JSONObject obj = JSON.parseObject(response);
		if (obj.getIntValue("status") == 0) {
			if (totlePage == -1) {
				int total = obj.getIntValue("total");
				totlePage = total / PAGE_SIZE
						+ ((total % PAGE_SIZE > 0) ? 1 : 0);
			}
			JSONArray groupArr = obj.getJSONArray("results");
			for (int i = 0; i < groupArr.size(); i++) {
				JSONObject groupObj = (JSONObject) groupArr.get(i);
				GrouponBean group = JSON.parseObject(groupObj.toJSONString(),
						GrouponBean.class);
				groupList.add(group);
			}
		}
		return groupList;
	}

	public static void main(String[] args) throws IOException {
		GrouponSearch search = new GrouponSearch();
		search.setQuery("美食");
		search.setPageNum(0);
		search.setLat(39.915f);
		search.setLng(116.404f);
		search.setRadius(2000);
		search.setFilter(false);
		ArrayList<GrouponBean> list = search.execute();
		Print.pln(TAG, list);
	}
}
